package com.liyunc.demo.controller;

import com.liyunc.demo.model.entity.Department;
import com.liyunc.demo.model.GeneralResponse;
import com.liyunc.demo.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
@RequestMapping(value = "/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @PostMapping(value = "save")
    public GeneralResponse saveDepartment(@RequestParam String name) {
        Department department = new Department();
        department.setName(name);

        log.info("save department");
        departmentService.saveDepartment(department);

        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("save success");
        return response;
    }

    @DeleteMapping(value = "delete")
    public GeneralResponse deleteDepartment(@RequestParam Long id) {
        log.info("delete department");
        departmentService.deleteDepartmentById(id);

        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("delete success");
        return response;
    }

}
